/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Controllers;

import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;

/**
 *
 * @author BENSON
 */
public class ControllerCreateReport {
    
    /**
     * variable and views declarations
     */
    
    Views.CreateDocReport createRep;
    Views.DoctorHomePage docHome;
    private ActionListener actionListener;
    Model.MainModel model;
    Connection conn=null;
    String querry;
    Statement st=null;
    String [] keyRay=null;
    
    
    /**
     * Default constructor for instantiating declared objects and views
     */
public ControllerCreateReport( ){
        createRep = new Views.CreateDocReport();
        createRep.setVisible(true);
        docHome=new Views.DoctorHomePage();
 }    
 
/**
 * Constructor to instantiate the array
 * @param array 
 */
public ControllerCreateReport(String[] array ){
       keyRay=array;
        createRep = new Views.CreateDocReport();
        createRep.setVisible(true);
        docHome=new Views.DoctorHomePage();
        
}/**
 * modify the report column of the database with the new inserted report
 * @param keyRay 
 */
public void createReport(String keyRay){
    try{
       Class.forName("com.mysql.jdbc.Driver");
       conn =(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/hosiManagerDb?zeroDateTimeBehavior=convertToNull","root","");
       querry = ("UPDATE Appointments SET doctorReports=?where appoinmentID = '" + keyRay + "';");
       PreparedStatement p =conn.prepareStatement(querry);
       p.setString(1, createRep.getTextArea().getText());
       p.execute();
               
       p.close();
       conn.close();
       JOptionPane.showMessageDialog(null, "Report has been created succesfully ");
    }  
    catch(ClassNotFoundException | SQLException | HeadlessException e){
        JOptionPane.showMessageDialog(null,e);
    } 
   
}

/**
 * Method to add actionListener to clicked buttons
 */

public void control(){        
        actionListener = new ActionListener() {
              @Override
              public void actionPerformed(ActionEvent actionEvent) {  
                  if(actionEvent.getSource()==createRep.getSaveBtn()){
                      System.out.println("The key is: "+keyRay[0]);
                      String key=keyRay[0];
                      System.out.println("The key is my  second: "+key);
                      createReport(key);
                  }
                  if(actionEvent.getSource()==createRep.getCloseBtn()){
                  createRep.setVisible(false);
                  
                  }
               }
              
        };                
        createRep.getSaveBtn().addActionListener(actionListener);
        createRep.getCloseBtn().addActionListener(actionListener);
        
}
        
    
}
